package com.bee.beemanage.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bee.beemanage.api.service.SysRolePermissionService;
import com.bee.beemanage.api.entity.SysPermission;
import com.bee.beemanage.api.entity.SysRole;
import com.bee.beemanage.api.entity.SysRolePermission;
import com.bee.beemanage.api.mapper.SysPermissionMapper;
import com.bee.beemanage.api.mapper.SysRoleMapper;
import com.bee.beemanage.api.mapper.SysRolePermissionMapper;
import com.bee.beemanage.system.common.utils.result.Result;
import com.bee.beemanage.system.common.utils.result.ResultCode;
import com.bee.beemanage.system.common.utils.result.ResultTool;
import com.bee.beemanage.system.common.utils.transaction.TransUtils;
import com.bee.beemanage.system.common.utils.transaction.Transaction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色权限关联表 服务实现类
 * </p>
 *
 * @author 陈国松
 * @since 2023-02-23
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements SysRolePermissionService {

    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysPermissionMapper sysPermissionMapper;
    @Resource
    SysRolePermissionMapper sysRolePermissionMapper;
    @Override
    public Result insert(SysRolePermission sysRolePermission) {
        Result result = new Result();
        Result errorCheck = allErrorCheck(sysRolePermission, result);
        if (errorCheck != null) return errorCheck;
        QueryWrapper<SysRolePermission> sysRolePermissionQueryWrapper = new QueryWrapper<>();
        sysRolePermissionQueryWrapper.eq("permission_id",sysRolePermission.getPermissionId());
        sysRolePermissionQueryWrapper.eq("role_id",sysRolePermission.getRoleId());
        SysRolePermission rolePermission = sysRolePermissionMapper.selectOne(sysRolePermissionQueryWrapper);
        if(null!=rolePermission){
             return ResultTool.fail(ResultCode.DATA_REPEAT);
        }
        sysRolePermissionMapper.insert(sysRolePermission);
        return ResultTool.success();
    }

    @Override
    public Result editBatch(String roleId,List<String> sysRolePermissions) {
        Result result = ResultTool.success();
        Transaction trans = null;
        try {
            trans = TransUtils.getTrans();
            trans.begin();
            List<SysRolePermission> sysRolePermission = new ArrayList<>();
            for (String rolePermissions : sysRolePermissions) {
                SysRolePermission sysRolePermissionT = new SysRolePermission();
                sysRolePermissionT.setPermissionId(rolePermissions);
                sysRolePermissionT.setRoleId(roleId);
                sysRolePermission.add(sysRolePermissionT);
            }
            QueryWrapper<SysRolePermission> query = Wrappers.query();
            query.eq("role_id",roleId);
            sysRolePermissionMapper.delete(query);
            saveBatch(sysRolePermission);
            trans.commit();
        } catch (Exception e) {
            if(null!=trans){
                trans.rollback();
            }
            result = ResultTool.fail();
            result.setErrorMsg(e.getMessage());
            return result;
        }
        return result;
    }

    @Override
    public Result removeByAll(SysRolePermission sysRolePermission) {
        Result result = ResultTool.success();
        Result errorCheck = allErrorCheck(sysRolePermission, result);
        if (errorCheck != null) return errorCheck;
        QueryWrapper<SysRolePermission> sysRolePermissionQueryWrapper = new QueryWrapper<>();
        sysRolePermissionQueryWrapper.eq("permission_id",sysRolePermission.getPermissionId());
        sysRolePermissionQueryWrapper.eq("role_id",sysRolePermission.getRoleId());
        SysRolePermission rolePermission = sysRolePermissionMapper.selectOne(sysRolePermissionQueryWrapper);
        if(null!=rolePermission){
            sysRolePermissionMapper.delete(sysRolePermissionQueryWrapper);
        }else {
            return ResultTool.fail(ResultCode.DATA_NOT_EXIST);
        }
        return result;
    }

    @Override
    public Result removeBatch(List<SysRolePermission> sysRolePermission) {
        Result result = ResultTool.success();
        Transaction trans = null;
        try {
            trans=TransUtils.getTrans();
            trans.begin();
            for (SysRolePermission rolePermission : sysRolePermission) {
                QueryWrapper<SysRolePermission> sysRolePermissionQueryWrapper = new QueryWrapper<>();
                sysRolePermissionQueryWrapper.eq("permission_id",rolePermission.getPermissionId());
                sysRolePermissionQueryWrapper.eq("role_id",rolePermission.getRoleId());
                sysRolePermissionMapper.delete(sysRolePermissionQueryWrapper);
            }
            trans.commit();
        } catch (Exception e) {
            if(null!=trans){
                trans.rollback();
            }
            result = ResultTool.fail();
            result.setErrorMsg(e.getMessage());
            return result;
        }
        return result;
    }

    @Override
    public List<SysRolePermission> selectListByRoleID(String roleId) {
        QueryWrapper<SysRolePermission> sysRolePermissionQueryWrapper = new QueryWrapper<>();
        sysRolePermissionQueryWrapper.eq("role_id",roleId);
        return sysRolePermissionMapper.selectList(sysRolePermissionQueryWrapper);
    }

    private Result allErrorCheck(SysRolePermission sysRolePermission, Result result) {
        if(null==sysRolePermission.getPermissionId()||null==sysRolePermission.getRoleId()){
            result.setSuccess(false);
            result.setErrorCode(ResultCode.PARAM_IS_BLANK.getCode());
            result.setErrorMsg("roleId与permissionId不能为空");
            return result;
        }
        QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
        sysRoleQueryWrapper.eq("id", sysRolePermission.getRoleId());
        SysRole sysRole = sysRoleMapper.selectOne(sysRoleQueryWrapper);
        if(null==sysRole){
            result.setSuccess(false);
            result.setErrorCode(ResultCode.PARAM_NOT_VALID.getCode());
            result.setErrorMsg("roleId 参数错误,请确保roleId为存在的角色Id");
            return result;
        }
        QueryWrapper<SysPermission> sysPermissionQueryWrapper = new QueryWrapper<>();
        sysPermissionQueryWrapper.eq("id", sysRolePermission.getPermissionId());
        SysPermission sysPermission = sysPermissionMapper.selectOne(sysPermissionQueryWrapper);
        if(null==sysPermission){
            result.setSuccess(false);
            result.setErrorCode(ResultCode.PARAM_NOT_VALID.getCode());
            result.setErrorMsg("permissionId 参数错误,请确保permissionId为存在的权限Id");
            return result;
        }
        return null;
    }
}
